node {
    stage('git chekout') {
        git branch: 'main', url: 'https://gitee.com/fastjrun/kafka-console-ui.git'
    }
    docker.image('maven:3.8.6-openjdk-11').inside("-v /opt/server/jenkins/data/.m2:/root/.m2"){
        stage('package') {
            sh 'JAVA_HOME=/usr/local/openjdk-11 sh package.sh'
        }
    }
    stage('prepare docker file') {
        sh 'mkdir -p output && rm -rf output/* && cp target/*.jar ./output ' +
                '&& cp Dockerfile ./output'
    }
    stage('dockerFile') {
        dir('output'){
            stash 'output'
        }
    }
    stage('parallel docker build') {
        parallel (
                'docker build && push arm64': {
                    node('arm64') {
                        dir('workdir'){
                            unstash 'output'
                        }
                        sh 'cd workdir && docker build . -t pi4k8s/kafka-console-ui:1.0-arm64'
                        sh 'docker push pi4k8s/kafka-console-ui:1.0-arm64'
                    }
                },
                'docker build && push amd64': {
                    node('amd64') {
                        dir('workdir'){
                            unstash 'output'
                        }
                        sh 'cd workdir && docker build . -t pi4k8s/kafka-console-ui:1.0-amd64'
                        sh 'docker push pi4k8s/kafka-console-ui:1.0-amd64'
                    }
                }
        )
    }
    stage('manifest'){
        try {
            sh "docker manifest rm pi4k8s/kafka-console-ui:1.0"
        }catch(exc){
            echo "some thing wrong"
        }
        sh "docker manifest create pi4k8s/kafka-console-ui:1.0 pi4k8s/kafka-console-ui:1.0-amd64 pi4k8s/kafka-console-ui:1.0-arm64"
        sh "docker manifest annotate pi4k8s/kafka-console-ui:1.0 pi4k8s/kafka-console-ui:1.0-amd64 --os linux --arch amd64"
        sh "docker manifest annotate pi4k8s/kafka-console-ui:1.0 pi4k8s/kafka-console-ui:1.0-arm64 --os linux --arch arm64"
        sh "docker manifest push pi4k8s/kafka-console-ui:1.0"
    }
}
